package cn.wqgallery.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bm.library.PhotoView;
import com.bumptech.glide.Glide;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    ViewPager viewPager;
    TextView textView;
    RelativeLayout ll_layout;
    RadioButton rb_left;
    ImageView iv_clear;

    private List<WorkBean> list = new ArrayList<>();
    private ImageAdapter imageAdapter;
    private int tag = 0;
    //视图
    private List<PhotoView> viewList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = findViewById(R.id.viewPager);
        textView = findViewById(R.id.tv_title);
        ll_layout = findViewById(R.id.ll_layout);

        rb_left = findViewById(R.id.rb_left);
        iv_clear=findViewById(R.id.iv_clear);
        init();

        rb_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setFinish();
            }
        });

        iv_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setClear();
            }
        });

    }

    private void init() {
//        List<WorkBean> imageList = (List<WorkBean>) getIntent().getSerializableExtra("viewList");
        list.clear();
//        list.addAll(imageList);
        WorkBean workBean1=new WorkBean("http://p2.so.qhimgs1.com/t011d6de038ad0b20b2.jpg");
        WorkBean workBean2=new WorkBean("http://p3.so.qhmsg.com/t0156743ae893f84248.jpg");
        WorkBean workBean3=new WorkBean("http://p0.so.qhimgs1.com/t01ed7deb8fc9db5f8a.jpg");

        list.add(workBean1);
        list.add(workBean2);
        list.add(workBean3);

        for (int i = 0; i < list.size(); i++) {
            PhotoView photoView = new PhotoView(this);
            photoView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            //开启缩放功能
            photoView.enable();
            //把图片设置到组件
            Glide.with(this).load(list.get(i).getImage()).into(photoView);
            //把组件装进集合
            viewList.add(photoView);

        }

        setData();


    }

    private boolean isOne = true;
    private int tagNum = 0;

    private void setData() {
        imageAdapter = new ImageAdapter(this, list, viewList);
        viewPager.setAdapter(imageAdapter);
        viewPager.setCurrentItem(tag);//显示当前显示的
        if (isOne) {
            textView.setText(1 + "/" + list.size());
        } else {
            textView.setText((tagNum + 1) + "/" + list.size());
            if (list.size() == 0) {
                textView.setText(0 + "/" + 0);
                ll_layout.setVisibility(View.VISIBLE);
            } else {
                ll_layout.setVisibility(View.GONE);
            }
            if (list.size() == 1) {
                textView.setText(1 + "/" + 1);
            }
        }


        //滑动监听
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tag = position;
                //设置显示数字
                tagNum = position;
                textView.setText(String.valueOf(tagNum + 1) + "/" + list.size());

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    protected void setFinish() {
        Intent intent = new Intent();
        intent.putExtra("image", (Serializable) list);

        setResult(RESULT_OK, intent);
        finish();

    }

    //点击删除
    private void setClear() {
        isOne = false;
        if (list.size() > 0) {
            //更新当前位置
            if (tagNum >= list.size() && tagNum >= 1) {
                tagNum--;
            }
            if (tagNum >= list.size()) {
                tagNum = list.size() - 1;
            }

            //防止最后一个报错
            if (list.size() == 1 && tag == 1) {
                tag--;
                textView.setText(0 + "/" + 0);
            }

            list.remove(tag);
            viewList.remove(tag);
            setData();


        }
    }

}
